package com.singletonapps.evory.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Version;

import com.singletonapps.evory.dto.CourseDto;
import com.singletonapps.evory.dto.CourseTemaDto;
import com.singletonapps.evory.dto.CourseVideoDto;
import com.singletonapps.evory.dto.MaterialDto;

@Entity
@Table(name = "COURSE")
public class Course implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5631330375878043413L;

	@Id
	@SequenceGenerator(name="course_seq", sequenceName="SEQ_COURSE_ID", allocationSize=1)
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "course_seq")
	@Column(name = "id")
	private Long id;
	
	@Column(name = "course_name")
	private String courseName;
	
	@Column(name = "course_desc")
	private String courseDescription;
	
	@Column(name = "course_image")
	private String courseImagePath;
	
	@Column(name = "course_themes")
	private String courseThemes;
	
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "curso")
	private List<CourseTema> temasCurso;
	
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "cursoMaterial")
	private List<Material> materialesCurso;
	
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "cursoVideo")
	private List<CourseVideo> videosCurso;
	
	@Column(name = "DATE_CREATE", nullable = false)
	private Date dateCreated;
	
	@Column(name = "DATE_UPDATE")
	private Date dateUpdated;
	
	@Version
	private int version;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getCourseName() {
		return courseName;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

	public String getCourseDescription() {
		return courseDescription;
	}

	public void setCourseDescription(String courseDescription) {
		this.courseDescription = courseDescription;
	}

	public String getCourseImagePath() {
		return courseImagePath;
	}

	public void setCourseImagePath(String courseImagePath) {
		this.courseImagePath = courseImagePath;
	}

	public Date getDateCreated() {
		return dateCreated;
	}

	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}

	public Date getDateUpdated() {
		return dateUpdated;
	}

	public void setDateUpdated(Date dateUpdated) {
		this.dateUpdated = dateUpdated;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public String getCourseThemes() {
		return courseThemes;
	}

	public void setCourseThemes(String courseThemes) {
		this.courseThemes = courseThemes;
	}

	public List<CourseTema> getTemasCurso() {
		return temasCurso;
	}

	public void setTemasCurso(List<CourseTema> temasCurso) {
		this.temasCurso = temasCurso;
	}
	
	public List<Material> getMaterialesCurso() {
		return materialesCurso;
	}

	public void setMaterialesCurso(List<Material> materialesCurso) {
		this.materialesCurso = materialesCurso;
	}
	
	public List<CourseVideo> getVideosCurso() {
		return videosCurso;
	}

	public void setVideosCurso(List<CourseVideo> videosCurso) {
		this.videosCurso = videosCurso;
	}

	public CourseDto toDto(){
		CourseDto dto = new CourseDto();
		
		dto.setId(this.id);
		dto.setNombre(this.courseName);
		dto.setDescripcion(this.courseDescription);
		dto.setCourseImagePath(this.courseImagePath);
		dto.setCourseThemes(this.courseThemes);
		
		if(this.temasCurso != null){
			List<CourseTemaDto> temas = new ArrayList<CourseTemaDto>();
			for (CourseTema t : this.temasCurso) {
				temas.add(t.toDto());
			}
			dto.setTemasCurso(temas);
		}
		
		if(this.materialesCurso != null){
			List<MaterialDto> materiales = new ArrayList<MaterialDto>();
			for (Material material : this.materialesCurso) {
				materiales.add(material.toDto());
			}
			dto.setMaterialesCurso(materiales);
		}
		
		if(this.videosCurso != null){
			List<CourseVideoDto> videos = new ArrayList<CourseVideoDto>();
			for (CourseVideo courseVideo : this.videosCurso) {
				videos.add(courseVideo.toDto());
			}
			dto.setVideosCurso(videos);
		}
		
		dto.setDateCreated(this.dateCreated);
		dto.setDateUpdated(this.dateUpdated);
		dto.setVersion(this.version);
		
		
		return dto;
	}
	
	
	
}
